package com.danieldura.highrise.data
{
	import com.adobe.utils.DateUtil;
	
	[Bindable]	
	public class Note implements HighriseData
	{
		public static const SUBJECT_PARTY:String = "Party";
		public static const SUBJECT_CASE:String = "Kase";

		public static const VISIBLE_EVERYONE:String = "Everyone";
		public static const VISIBLE_OWNER:String = "Owner";
		public static const VISIBLE_NAMEDGROUP:String = "NamedGroup";
				
		public var id:int;
		public var subjectId:int;
		public var subjectType:String;
		public var body:String;
		public var caseId:int;
		public var updatedAt:Date;
		public var createdAt:Date;
		public var visibleTo:String;
		public var ownerId:int;
		public var groupId:int;
		public var authorId:int;
		public var attachments:Array;
		
		public function parse( data:XML ):void
		{
			id = data.id;
			subjectId = data["subject-id"];
			subjectType = data["subject-type"];
			body = data.body;
			caseId = data["kase-id"];
			updatedAt = DateUtil.parseW3CDTF( data["updated-at"] );
			createdAt = DateUtil.parseW3CDTF( data["created-at"] );
			visibleTo = data["visible-to"];
			ownerId = data["owner-id"];
			groupId = data["group-id"];
			authorId = data["author-id"];
			
			attachments = new Array();
			for each( var attachmentXML:XML in data.attachments.attachment )
			{
				var attachment:Attachment = new Attachment();
				attachment.parse( attachmentXML );
				attachments.push( attachment );
			}
		}
		
		public function toXML():XML
		{
			var data:XML = new XML( <note /> );
			data.id = id;
			data["subject-id"] = subjectId;
			data["subject-type"] = subjectType;
			data.body = body;
			data["kase-id"] = caseId;
			data["visible-to"] = visibleTo;
			data["group-id"] = groupId;
			return data;
		}
		
		public function toXMLString():String
		{
			return toXML().toXMLString();
		}
		
	}
}

/*
<note>
  <id type="integer">1</id>
  <subject-id type="integer">1</subject-id>
  <subject-type>#{ Party || Kase }</subject-type>
  <body>Hello world!</body>
  <kase-id type="integer">1</kase-id>
  <updated-at type="datetime">2007-02-27T18:42:28Z</updated-at>
  <created-at type="datetime">2006-05-16T17:26:00Z</created-at>
  <visible-to>#{Everyone || Owner || NamedGroup}</visible-to>
  <owner-id type="integer">#{ user_id -- requires visble-to is "Owner"}</owner-id>
  <group-id type="integer">#{ group_id -- requires visble-to is "NamedGroup"}</group-id>
  <author-id type="integer">3</author-id>
  <attachments>
    <attachment>
      <id type="integer">1</id>
      <url>http://example.highrisehq.com/.../picture.png</url>
    </attachment>
    <attachment>
      <id type="integer">2</id>
      <url>http://example.highrisehq.com/.../document.txt</url>
    </attachment>
  </attachments>
</note>
*/